// pages/range_1/range_1.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:[],
    array_class:[],
    item:'',
    bg_img:app.globalData.bg_img,
    // imgPath1:"https://thirdwx.qlogo.cn/mmopen/vi_32/PIrtJicRCoFvXmnPYKyCs6PvWHDTjBsOia4ibTI74JkGjlYjMe2Mf9fIpRiazsY2RzJY6C1kOAcjmWiaLLFFhQ0d9vQ/132"
    // imgPath1:'../../pages/icon/guanbi.png'
    imgPath1:""
  },


  //获取头像
  getImg(){
    wx.request({
      url: 'url',
      success:(res)=>{
        res.data.src
      }
    })
  },


//获取类别函数
getClass(musicClass){
  let str = "" ;
  switch(musicClass){

   
    case 'yg':
     str = "摇滚"
      break;
    case 'sc':
     str = "说唱"
      break;
    case 'my':
     str = "民谣"
      break;
    case 'lx':
     str = "流行"
      break;
  }

    this.data.array.map(item=>{
      if(item.Class == str){
        this.data.array_class.push(item)
        const  arr = this.data.array_class
        this.setData({
          array_class:arr
        })
      }
    })
},


  //已存在的用户帖子数据
postPast(musicClass){
  var that = this
wx.request({
  url: 'http://49.233.35.127/admin/editor',
  success:function(result){
    console.log('请求的数据：',result.data);
    that.setData({
      array:result.data
    })
    that.getClass(musicClass)
  }
})
},

//上传帖子至数据库
addList(userName,time,leibie){
  const that = this
  wx.request({
    url: 'http://49.233.35.127/Wx/editor/add',
    method:'POST',
    data:{
      text:that.data.item,
      username:userName,
      time:time,
      Class:leibie
    },
    success:(res)=>{
      console.log(res.data[0]['max(ID)'])
      that.setData({
        delID:res.data[0]['max(ID)']
      })
    }
  })
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.postPast(options.class);
    const cl = "摇滚"
    if(options.content){
   //   console.log(options);
      this.setData({
        item:options.content
      })

      const userName = app.globalData.userInfo['userName'];
      
        switch(options.class){
          case 'yg':
            var leibie = "摇滚"
            break;
          case 'my':
            var leibie = "民谣"
            break;
          case 'lx':
            var leibie = "流行"
            break;
          case 'sc':
            var leibie = "说唱"
            break;
        }  
      const myTime = new Date();
      const time = myTime.getFullYear() + "-" + (myTime.getMonth()+1) + "-" +myTime.getDate() +" " + myTime.getHours() + ":"+ myTime.getMinutes()+ ":"+ myTime.getSeconds()
      console.log(time,userName,leibie)

      //发帖子给数据库
      this.addList(userName,time,leibie)

    }
    
   
//获得用户信息
 //   console.log(app.globalData.userInfo)
  },
 
   
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      bg_img:app.globalData.bg_img
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})